<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 14-10-23
 * Time: 下午1:36
 * @author 郑钟良<zzl@ourstu.com>
 */

namespace Admin\Controller;


use Admin\Builder\AdminListBuilder;

class PeopleController
{

    /**校园成员列表
     * @param int $page
     * @author 郑钟良<zzl@ourstu.com>
     */
    public function member($page = 1)
    {
        //获取校园成员id
        $expectIds = D('AuthGroupAccess')->where(array('group_id' => 2))->select(); //group_id=2是校园用户组id
        if (is_array($expectIds) && count($expectIds)) {
            $expectIds = array_column($expectIds, 'uid');
            $map['uid'] = array('not in', $expectIds); //排除校园成员
        }
        $map['is_production'] = 0; //排除官方制片成员
        $map['status'] = 1;
        $map['last_login_time'] = array('neq', 0);
        $model = D('Member');
        $memberIds = $model->where($map)->page($page, 50)->field('uid')->select();
        $totalCount = $model->where($map)->count();
        $memberList = array();
        foreach ($memberIds as $v) {
            $memberList[] = query_user(array('id', 'uid', 'reg_time', 'last_login_time', 'email', 'is_recommend', 'fans', 'doing', 'address', 'sentiment', 'help_count'), $v['uid']);
        }
        unset($v);
        $builder = new AdminListBuilder();

        $builder->title('校园用户列表')->meta_title = '校园用户列表';
        $builder->ajaxButton(U('admin/people/setRecommend'), array('is_recommend' => 1), '设为推荐')
            ->keyUid()->keyTime('reg_time', '注册时间')->keyTime('last_login_time', '最后登录时间')->keyText('email', '邮箱')->keyYesNo('is_recommend', '是否推荐')->keyText('fans', '粉丝数')->keyText('doing', '职业')->keyText('address', '居住地址')->keyText('sentiment', '人气')->keyText('help_count', '帮忙数');
        $builder->pagination($totalCount, 50)->data($memberList)
            ->display();
    }

    /**推荐成员列表
     * @param int $page
     * @author 郑钟良<zzl@ourstu.com>
     */
    public function recommendMember($page = 1)
    {
        //获取校园成员id
        $expectIds = D('AuthGroupAccess')->where(array('group_id' => 2))->select(); //group_id=2是校园用户组id
        if (is_array($expectIds) && count($expectIds)) {
            $expectIds = array_column($expectIds, 'uid');
            $map['uid'] = array('not in', $expectIds); //排除校园成员
        }
        $map['is_production'] = 0; //排除官方制片成员
        $map['status'] = 1;
        $map['last_login_time'] = array('neq', 0);
        $map['is_recommend'] = 1;
        $model = D('Member');
        $recommendIds = $model->where($map)->page($page, 50)->field('uid')->select();
        $totalCount = $model->where($map)->count();
        $memberList = array();
        foreach ($recommendIds as $v) {
            $memberList[] = query_user(array('id', 'uid', 'reg_time', 'last_login_time', 'email', 'is_recommend', 'fans', 'doing', 'address', 'sentiment', 'help_count'), $v['uid']);
        }
        unset($v);
        $builder = new AdminListBuilder();

        $builder->title('校园用户列表')->meta_title = '校园用户列表';
        $builder->ajaxButton(U('admin/campus/setRecommend'), array('is_recommend' => 0), '取消推荐')
            ->keyUid()->keyTime('reg_time', '注册时间')->keyTime('last_login_time', '最后登录时间')->keyText('email', '邮箱')->keyYesNo('is_recommend', '是否推荐')->keyText('fans', '粉丝数')->keyText('doing', '职业')->keyText('address', '居住地址')->keyText('sentiment', '人气')->keyText('help_count', '帮忙数');
        $builder->data($memberList)->pagination($totalCount, 50)
            ->display();
    }

    /**设置、取消推荐
     * @param string $ids
     * @param int $is_recommend
     * @author 郑钟良<zzl@ourstu.com>
     */
    public function setRecommend($ids = '', $is_recommend = 1)
    {
        if (!is_array($ids) || !count($ids)) {
            $this->error('请勾选要设置的项！');
        }
        D('Member')->where(array('uid' => array('in', $ids)))->setField('is_recommend', $is_recommend);
        foreach ($ids as $val) {
            clean_query_user_cache($val, 'is_recommend');
        }
        $this->success('设置成功', $_SERVER['HTTP_REFERER']);
    }
} 